package com.jxwifi.cloud.quickclean.login;

import android.content.Context;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.idroid.widget.Toaster;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.InfoBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.AuzHttp;
import com.jxwifi.cloud.quickclean.okhttp.Params;

import cn.trinea.android.common.util.REPattern;

public class BindPhonePresenter {

    private Context mContext;
    private BindPhoneView mView;
    private AuzImpl mAuz;

    private CountDownTimer timer;

    public BindPhonePresenter(Context context,BindPhoneView view){
        this.mContext=context;
        this.mView=view;
        mAuz=new AuzImpl();
    }

    public void ChangedPhone(){
        mView.getBindPhone().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                TextNull();
            }
        });
    }

    public void ChangedVerification(){
        mView.getBindVerification().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                TextNull();
            }
        });
    }

    public void validation(){
        if ( mView.getBindPhone().getText().toString().equals("")) {
            Toaster.show(mContext, "请输入手机号码");
            return;
        }
        boolean isMobile = REPattern.isMobileNO( mView.getBindPhone().getText().toString());
        if (!isMobile) {
            Toaster.show(mContext, "请输入正确的手机号码");
            return;
        }
        setTimerCount();
        SendIdentCode();
    }

    //倒计时操作
    private void setTimerCount() {
        timer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mView.getVer().setText("验证码(" + millisUntilFinished / 1000 + "s)");
                mView.getVer().setEnabled(false);
            }

            @Override
            public void onFinish() {
                timer.cancel();
                mView.getVer().setEnabled(true);
                mView.getVer().setText("重获验证码");
            }
        };
        timer.start();
    }

    private void SendIdentCode(){
        Params params=new Params();
        params.add("mobile",mView.getBindPhone().getText().toString());
        params.add("type", "CUSTOMER_LOGIN");
        mAuz.auzget(UrlPool.POST_CAPTCHA_VALIDATE, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {

            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode,msg);
            }
        });
    }

    public void judge(){
        if (mView.getBindPhone().getText().toString().equals("")) {
            Toaster.show(mContext, "请输入手机号码");
            return;
        }
        boolean isMobile = REPattern.isMobileNO(mView.getBindPhone().getText().toString());
        if (!isMobile) {
            Toaster.show(mContext, "请输入正确的手机号码");
            return;
        }
        if (mView.getBindVerification().getText().toString().equals("")) {
            Toaster.show(mContext, "验证码不能为空");
            return;
        }
        logining();
    }

    public void logining(){
        mView.getLoginButton().setClickable(false);
        mView.getLoginButton().setBackgroundResource(R.drawable.shap_bottom_buttom_no);
        Params params=new Params();
        params.add("code",mView.getBindVerification().getText().toString());
        params.add("tell",mView.getBindPhone().getText().toString());
        mAuz.auzget(UrlPool.UPDATE_TELL, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                JSONObject obj = JSON.parseObject(data);
                String token=obj.getString("token");
                AccountMgr.puttoken(mContext,token);
                AuzHttp.KJB_APP_Token=AccountMgr.gettoken(mContext);  //token添加到请求头
                String userInfo=obj.getString("userInfo");
                InfoBean mInfoBean=JSON.parseObject(userInfo,InfoBean.class);

                Log.w("oushuhua","login Token="+ AuzHttp.KJB_APP_Token);
                AccountMgr.putUser(mContext,mInfoBean,true);
                AccountMgr.loginSuccess(mContext);
                mView.getLoginButton().setClickable(true);
                mView.getLoginButton().setBackgroundResource(R.drawable.shap_bottom_buttom_selector);
                mView.onSuccessIntent();
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode,msg);
            }
        });
    }

    public void TextNull(){
        if (!TextUtils.isEmpty(mView.getBindPhone().getText().toString())&&
                !TextUtils.isEmpty(mView.getBindVerification().getText().toString())&&mView.getIsSelection()){
            mView.getLoginButton().setBackgroundResource(R.drawable.shap_bottom_buttom_selector);
            mView.getLoginButton().setClickable(true);
        }else{
            mView.getLoginButton().setBackgroundResource(R.drawable.shap_bottom_buttom_no);
            mView.getLoginButton().setClickable(false);
        }
    }
}
